package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.DataResult;
import com.dongdong.data.ResponseStatusEnum;
import com.dongdong.pojo.*;
import com.dongdong.service.AddressService;
import com.dongdong.service.AreasService;
import com.dongdong.service.CitiesService;
import com.dongdong.service.ProvincesService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/22 16:49
 * @Version 1.0
 **/
@Controller
@RequestMapping("addressController")
public class AddressController {

    private static final Logger logger = LoggerFactory.getLogger(ContentController.class);

    @Reference
    private AddressService addressService;

    @Reference
    private AreasService areasService;

    @Reference
    private CitiesService citiesService;

    @Reference
    private ProvincesService provincesService;

    @RequestMapping("toHomeIndex")
    public String toHomeIndex(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user != null){
            return "home-index";
        }else {
            return "login";
        }
    }

    @RequestMapping("toAddress")
    public String toAddress(Model model){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",user);
        return "home-setting-address";

    }

    @RequestMapping("getAddress")
    @ResponseBody
    public DataResult getAddress(){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            List<TbAddress> addressList = addressService.getAddress(user.getUsername());
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(addressList);
        }catch (Exception e){
            logger.error("查询错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getProvinces")
    @ResponseBody
    public DataResult getProvinces(){
        try {
            List<TbProvinces> provincesList = provincesService.getProvinces();
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(provincesList);
        }catch (Exception e){
            logger.error("查询省错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getCity")
    @ResponseBody
    public DataResult getCity(String provinceid){
        try {
            List<TbCities> citiesList = citiesService.getCity(provinceid);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(citiesList);
        }catch (Exception e){
            logger.error("查询市错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getTown")
    @ResponseBody
    public DataResult getTown(String cityId){
        try {
            List<TbAreas> areasList = areasService.getTown(cityId);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(areasList);
        }catch (Exception e){
            logger.error("查询市错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertAddress")
    @ResponseBody
    public DataResult insertAddress(TbAddress address){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            address.setUserId(user.getUsername());
            addressService.insertAddress(address);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }catch (Exception e){
            logger.error("查询市错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteAddress/{id}")
    @ResponseBody
    public DataResult deleteAddress(@PathVariable("id") Long id){
        try {
            addressService.deleteAddress(id);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }catch (Exception e){
            logger.error("删除错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("isDefault/{id}")
    @ResponseBody
    public DataResult isDefault(@PathVariable("id") Long id){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            addressService.isDefault(user.getUsername(),id);
            return DataResult.response(ResponseStatusEnum.SUCCESS);
        }catch (Exception e){
            logger.error("修改错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }
}

